import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Test2 {

    public static void main(String[] args) {

    }
    //全排列Ⅱ
    List<List<Integer>> ret = new ArrayList<>();//存放最终结果
    List<Integer> path = new ArrayList<>();//存放一个排列组合
    Set<ArrayList<Integer>> set=new HashSet<>();//查重
    boolean[] check;
    public List<List<Integer>> permuteUnique(int[] nums) {
        check = new boolean[nums.length];
        dfs(nums);
        return ret;
    }
    public void dfs(int[] nums) {
        //走到叶子结点
        if(nums.length == path.size()) {
            if(!set.contains(new ArrayList(path))) {
                ret.add(new ArrayList(path));
                set.add(new ArrayList(path));

            }
            return;
        }
        for(int i = 0;i<nums.length;i++) {
            if(check[i] == false) {
                path.add(nums[i]);
                check[i] = true;
                dfs(nums);
                //回溯
                path.remove(path.size()-1);
                check[i] = false;
            }
        }

    }

}
